package fr.ubdx1.jacomode.controller;

/*
 *  Copyright (C) 2010 Jonathan MERCIER <bioinfornatics@fedoraproject.org>,
 *  Louise-Amélie SCHMITT <louise-amelie.schmitt@etu.u-bordeaux1.fr>,
 *  Florence Maurier <florence_maurier@orange.fr>,
 *  Marianne Dussart <marianne.dussart@orange.fr>,
 *  Nicolas Fontaine <nifontaine@gmail.com>
 * 
 *  JACoMode is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  JACoMode is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with JACoMode.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.IOException;
import javax.servlet.ServletException;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;
import java.util.regex.Pattern;

/**
 *
 * @author Jonathan MERCIER <bioinfornatics@fedoraproject.org>,Louise-Amélie SCHMITT <louise-amelie.schmitt@etu.u-bordeaux1.fr>,Florence Maurier <florence_maurier@orange.fr>,Marianne Dussart <marianne.dussart@orange.fr>,Nicolas Fontaine <nifontaine@gmail.com>
 */
public class Content
{
    private static final String[] PAGES = {"about","acomForm","acomGlobalResults", "acomResultsByEFM", "acomResultsByMotifSize", "bilanForm", "bilanResult", "bilanSearch", "compareForm", "compareResult", "error", "main", "nomotif", "resultTable","selectWichAcomSession"};

    /**
     * Returns URL of the Web page.
     * @param webPage page name
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     * @return a String containing Web page URL or error URL
     */
    public String getInclude(String webPage) throws ServletException, IOException
    {
        WebContext  wctx            = WebContextFactory.get();
        boolean     isFound         = false;
        int         iterator        = 0;

        while (iterator < PAGES.length)
        {
            if (Pattern.matches(webPage,PAGES[iterator] ) )
            {
                isFound = true;
                iterator = PAGES.length;
            }
            else
            {
                iterator++;
            }
        }

        if ( isFound )
        {
            return wctx.forwardToString( createURL(webPage) );
        }
        else
        {
            return wctx.forwardToString( createURL("error.jsp") );
        }
    }

    /**
     * Returns a complete URL
     * @param url Web page name
     * @return a String containing Web page URL
     */
    private String createURL(String url)
    {
        return "/pages/"+url+".jsp";
    }

//    public String getWebPage()
//    {
//        return webPage;
//    }

//    public void setWebPage(String url)
//    {
//        webPage = url;
//    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    public String getServletInfo()
    {
        return "servlet comunicate with javascript";
    }
}
